package com.xbongbong.saas.service;

import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.SheetTypeEnum;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.enums.UpdateCustomerTeamScenesEnum;
import com.xbongbong.pro.listbatch.pojo.dto.BatchAddUserDTO;
import com.xbongbong.pro.listbatch.pojo.dto.ChangeMainDTO;
import com.xbongbong.pro.listbatch.pojo.dto.HandoverDTO;
import com.xbongbong.pro.product.pojo.dto.BeforeSaveMyTeamDTO;
import com.xbongbong.pro.userteam.pojo.dto.UserTeamAddDTO;
import com.xbongbong.pro.userteam.pojo.dto.UserTeamAddPushDTO;
import com.xbongbong.pro.userteam.pojo.dto.UserTeamBatchSaveDTO;
import com.xbongbong.pro.userteam.pojo.dto.UserTeamDTO;
import com.xbongbong.pro.userteam.pojo.dto.UserTeamDelDTO;
import com.xbongbong.pro.userteam.pojo.dto.UserTeamDelPushDTO;
import com.xbongbong.pro.userteam.pojo.dto.VerifyUserTeamForImportDTO;
import com.xbongbong.saas.domain.entity.ClueRuleEntity;
import com.xbongbong.saas.domain.entity.CustomerRuleEntity;
import com.xbongbong.saas.domain.entity.CustomerUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.elasticsearch.action.support.WriteRequest;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 协同团队
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/19 18:55
 * @since v1.0
 */
public interface UserTeamService {

    BaseVO add(UserTeamAddDTO userTeamAddDTO) throws XbbException;

    /**
     * 批量解散客户团队
     * @param corpid
     * @param customerIdList
     */
    void disbandedCustomerTeam(String corpid, List<Long> customerIdList) throws XbbException;

    void disbandedOtherUser(String corpid, List<Long> customerIdList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 删除团队成员
     * @param userTeamDelDTO
     * @return
     */
    BaseVO delete(UserTeamDelDTO userTeamDelDTO) throws XbbException;

    /**
     * 获取协同团队
     *
     * @param dataIdList id集合
     * @param corpid 公司id
     * @param businessType 类型
     * @return 团队集合
     */
    List<UserTeamEntity> batchGetUserTeam(List<Long> dataIdList, String corpid, Integer businessType);

    /**
     * 列表获取团队负责人
     * @param dataIdList 数据idIn
     * @param corpid 公司id
     * @param businessType 业务类型
     * @param isPublic 是否公海，基本传false
     * @return 数据对应的负责人map
     */
    Map<Long,String> getMainUserMap(Collection<Long> dataIdList, String corpid, Integer businessType, Boolean isPublic);

    /**
     * 列表获取团队负责人
     * @param dataIdList 数据idIn
     * @param corpid 公司id
     * @param businessType 业务类型
     * @param isPublic 是否公海，基本传false
     * @return 数据对应的负责人map
     */
    Map<Long, List<UserTeamEntity>> getMainUserTeamListMap(Collection<Long> dataIdList, String corpid, Integer businessType, Boolean isPublic);
    /**
     * 列表获取协同人负责人
     * @param dataIdList 数据idIn
     * @param corpid 公司id
     * @param businessType 业务类型
     * @return 数据对应的负责人map
     */
    Map<Long, List<UserTeamEntity>> getCoUserMap(Collection<Long> dataIdList, String corpid, Integer businessType);

    /**
     * 获取产品负责人或负责部门
     * @param dataIdList 数据idIn
     * @param corpid 公司id
     * @param businessType 业务类型
     * @param type 0员工,1部门
     * @return 数据对应的负责人或负责部门map
     */
    Map<Long, String> getProductUserMap(Collection<Long> dataIdList, String corpid, Integer businessType,Integer type);

    /**
     * 获取负责团队
     * @param dataId 数据id
     * @param corpid 公司id
     * @param businessType 类型
     * @return 团队
     */
    Map<Integer, List<UserTeamEntity>> getUserTeam(Long dataId, String corpid, Integer businessType);

    /**
     * 获取团队数目是否大于0
     *
     * @param corpid 公司id
     * @param userId 员工id
     * @param dataId 数据id
     * @param businessType 业务类型
     * @param isMain 是否主负责人
     * @return 是否大于0
     */
    boolean getUserTeamList4CheckBeyondZero(String corpid, String userId, Long dataId, Integer businessType, Integer isMain);

    /**
     * 联系人团队
     *
     * @param corpid
     * @param userId
     * @param contactId
     * @param customerId
     * @param isMain
     * @return BaseVO
     * @throws XbbException
     */
    BaseVO createContactUser(String corpid, String userId, Long customerId, Long contactId, Integer isMain) throws XbbException;

    /**
     * 客户团队
     *
     * @param corpid
     * @param userId
     * @param customerId
     * @param isMain
     * @return BaseVO
     * @throws XbbException
     */
    BaseVO createCustomerUser(String corpid,String userId,Long customerId, Integer isMain) throws XbbException;
    /**
     * 合同团队
     *
     * @param corpid
     * @param userId
     * @param contractId
     * @return BaseVO
     * @throws XbbException
     */
    BaseVO createContractUser(String corpid, String userId, Long contractId, Integer isMain, Long parentId) throws XbbException;


    BaseVO createOpportunityUser(String corpid, String userId, Long opportunityId, Integer isMain, Long dataId) throws XbbException;

    BaseVO createQuotationUser(String corpid, String userId, Long quotationId, Integer isMain, Long customerId) throws XbbException;

    BaseVO createBomBillUser(String corpid,String userId,Long bomBillId, Integer isMain) throws XbbException;

    BaseVO createSupplierUser(String corpid, String userId, Long supplierId, Integer isMain) throws XbbException;

    BaseVO createProductionOrderUser(String corpid, String userId, Long productionOrderId, Integer isMain) throws XbbException;

    BaseVO createPurchaseUser(String corpid, String userId, Long purchaseId, Integer isMain) throws XbbException;

    BaseVO createPaymentUser(String corpid, Long customerId, Long contractId, List<Long> paymentIdIn, Integer paymentLinkType, Boolean parentNew) throws XbbException;

    BaseVO createPayPlanUser(String corpid, Long supplierId, Long purchaseId, List<Long> payPlanIdIn, boolean parentNew) throws XbbException;

    BaseVO createPaymentSheetUser(String corpid, Long customerId, List<Long> contractIdIn, Map<Long, List<Long>> sheetIdAndDataIdMap, SheetTypeEnum sheetTypeEnum, Boolean parentNew) throws XbbException;

    BaseVO createPaySheetUser(String corpid, Long customerId, List<Long> contractIdIn, Map<Long, List<Long>> sheetIdAndDataIdMap, SheetTypeEnum sheetTypeEnum, Boolean parentNew) throws XbbException;


    BaseVO createSupplierContactUser(String corpid, Long supplierId, List<Long> contactIdIn) throws XbbException;

    boolean updateCustomerTeam(UserEntity userEntity, List<Long> customerIdList, UpdateCustomerTeamScenesEnum updateCustomerTeamScenesEnum, Long formId, Integer saasMark, Integer businessType, Integer distributorMark,String groupId) throws XbbException;

    boolean updateClueTeam(UserEntity user, List<Long> clueIdList, UpdateCustomerTeamScenesEnum updateCustomerTeamScenesEnum, Long formId, Integer saasMark, Integer businessType, Integer distributorMark,String groupId) throws XbbException;

    /**
     * 协同团队删除成员消息推送
     *
     * @param  userTeamDelPushDTO 删除成员消息推送入参
     * @author GuoJun.HU
     * @since V1.0
     * @date 2019/4/17 15:38
     * @throws XbbException XbbException
     */
    void pushMessageAfterRemoveUser(UserTeamDelPushDTO userTeamDelPushDTO) throws XbbException;

    /**
     * 协同团队添加成员消息推送
     *
     * @param  userTeamAddPushDTO 添加成员消息推送入参
     * @author GuoJun.HU
     * @since V1.0
     * @date 2019/4/17 15:38
     * @throws XbbException XbbException
     */
    void pushMessageAfterAddUser(UserTeamAddPushDTO userTeamAddPushDTO) throws XbbException;

    /**
     * 负责更新时校验处理
     * @param dataIdList 业务数据idList
     * @param corpid 公司id
     * @param bsuinessType 业务类型
     * @param isPublic 是否公海(有个前负责人逻辑
     * @param mainUserMap 负责人map
     * @param coUserMap 协同人map
     */
    void getUserIdMap(List<Long> dataIdList, String corpid, Integer bsuinessType, Boolean isPublic, Map<Long,List<String>> mainUserMap, Map<Long,List<String>> coUserMap);

    /**
     * 负责更新时校验处理(该方法与getUserIdMap()方法的区别在于将delUserIdList以及其的判断去除(因为getUserIdMap()方法用到的地方比较多，所以重新写了一个))
     * @param dataIdList 业务数据idList
     * @param corpid 公司id
     * @param bsuinessType 业务类型
     * @param isPublic 是否公海(有个前负责人逻辑
     * @param mainUserMap 负责人map
     * @param coUserMap 协同人map
     */
    void getAnotherUserIdMap(List<Long> dataIdList, String corpid, Integer bsuinessType, Boolean isPublic, Map<Long,List<String>> mainUserMap, Map<Long,List<String>> coUserMap);

    /**
     * 负责展示处理
     * @param userIdList
     * @param dataIdList 业务数据idList
     * @param corpid 公司id
     * @param bsuinessType 业务类型
     * @param isPublic 是否公海(有个前负责人逻辑
     * @param mainUserMap 负责人map
     * @param coUserMap 协同人map
     */
    void getUserIdMapSetShow(Set<String> userIdList,List<Long> dataIdList, String corpid, Integer bsuinessType, Boolean isPublic, Map<Long,List<String>> mainUserMap, Map<Long,List<String>> coUserMap);
    
    /**
     *
     * @param dataIdList 业务数据idList
     * @param corpid 公司id
     * @param bsuinessType 业务类型
     * @param isPublic 是否公海(有个前负责人逻辑
     * @param mainUserMap 负责人map
     * @param coUserMap 协同人map
     */
    void getUserMap(List<Long> dataIdList, String corpid, Integer bsuinessType, Boolean isPublic, Map<Long,String> mainUserMap, Map<Long,String> coUserMap);

    /**
     * 移交删除客户团队
     * @param corpid
     * @param removeIdList
     */
    void handoverRemoveUser(String corpid, List<Long> removeIdList) throws XbbException;

    /**
     * 移交新增客户团队
     * @param corpid
     * @param insertBatchList
     * @param userId
     */
    void handoverInsertUser(String corpid, List<CustomerUserEntity> insertBatchList, String userId) throws XbbException;


    /**
     * Description: 移交新增团队
     * @param insertBatchList
     * @param handoverDTO
     * @author 刘阳
     * @date 2020/1/9 2:04 PM
     * @since v1.0
     */
    void handoverInsertUser(List<UserTeamEntity> insertBatchList, HandoverDTO handoverDTO, Map<Long, PaasFormDataEntityExt> entityMap, Map<String, UserEntity> userEntityMap, List<Long> dataIds) throws XbbException;

    void updateUserTeamByCustomer(List<Long> customerIdList, String corpid, UserTeamEntity userTeamEntity) throws XbbException;


    /**
     * 校验负责团队
     *
     * @param userTeamDTO 入参
     * @throws XbbException 异常
     */
    BeforeSaveMyTeamDTO verifyMyTeam(UserTeamDTO userTeamDTO) throws XbbException;

    /**
     * 保存负责团队
     *
     * @param userTeamDTO 入参
     * @throws XbbException 异常
     */
    void saveMyTeam(UserTeamBatchSaveDTO userTeamBatchSaveDTO) throws XbbException;


    /**
     * 获取可见的部门和可见的人
     * @param userIdList
     * @param corpid
     * @param productId
     * @param visableDep
     * @param visableUser
     */
    void getVisableDepAndUser(Set<String> userIdList, String corpid,Long productId,List<String> visableDep,List<String> visableUser);

    /**
     * 批量获取userTeam
     *
     * @param dataIdList
     * @param corpid
     * @param businessType
     * @param isPublic
     * @param mainUserMap
     * @param coUserMap
     */
    void batchGetUserTeamMap(List<Long> dataIdList, String corpid, Integer businessType, Boolean isPublic, Map<Long, List<UserTeamEntity>> mainUserMap, Map<Long, List<UserTeamEntity>> coUserMap);

    void verifyMyTeamForImport(VerifyUserTeamForImportDTO verifyUserTeamForImportDTO) throws XbbException;

    void beforeVerifyUserTeam(ValidateDataDTO validateDataDTO) throws XbbException;

    UserTeamBatchSaveDTO beforeSaveMyTeam(BeforeSaveMyTeamDTO beforeSaveMyTeamDTO) throws XbbException;

    void saveUserTeam(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;

    /**
     * 检查是否有权限新增删除团队（协同人）
     * @param validateDataDTO
     */
    void beforeWorkOrderUserTeam(ValidateDataDTO validateDataDTO) throws XbbException;

    String canGainMaxNum(CustomerRuleEntity rule, String corpid,
                              UserEntity userEntity, Map<Long, PaasFormDataEntityExt> entityExtMap) throws XbbException;

    String canGainRate(CustomerRuleEntity rule, String corpid, UserEntity userEntity, Integer dataSize) throws XbbException;

    List<String> canGainPrePrincipal(CustomerRuleEntity rule, String corpid,
                               UserEntity userEntity, List<Long> dataIds, Map<Long, PaasFormDataEntityExt> entityExtMap, String operateStr) throws XbbException;

    String canGainMaxClueNum(ClueRuleEntity rule, String corpid,
                             UserEntity userEntity, Map<Long, PaasFormDataEntityExt> entityExtMap) throws XbbException;

    String canGainClueRate(ClueRuleEntity rule, String corpid, UserEntity userEntity, Integer dataSize) throws XbbException;

    List<String> canGainCluePrePrincipal(ClueRuleEntity rule, String corpid,
                                     UserEntity userEntity, List<Long> dataIds, Map<Long, PaasFormDataEntityExt> entityExtMap, String operateStr) throws XbbException;

    /**
     * 修复回款单/付款单的负责人数据
     * @param corpid
     * @param businessTypeEnum
     * @param paasFormDataList
     * @throws XbbException
     */
    void scriptUserTeamBySheetBusiness(String corpid, XbbRefTypeEnum businessTypeEnum, List<PaasFormDataEntityExt> paasFormDataList) throws XbbException;

    void batchAddUserTeam(BatchAddUserDTO batchAddUserDTO) throws XbbException;


    void handoverUpdateOwner(ChangeMainDTO changeMainDTO) throws XbbException;

    void updateSheetTeam(String corpid, XbbRefTypeEnum xbbRefTypeEnum, Long dataId) throws XbbException;
}
